﻿using System.Text.Json;

namespace GameDataParser;

public class VideoGamesDeserialize:IVideoGamesDeserialize
{
    private readonly IUserInteractor _userInteractor;
    public VideoGamesDeserialize(IUserInteractor userInteractor)
    {
        _userInteractor = userInteractor;
    }

    public  List<VideoGame> DeserializeVideoGames(string fileName, string fileContents)
    {
        List<VideoGame> videoGames = default;
        try
        {
            return JsonSerializer.Deserialize<List<VideoGame>>(fileContents);
        }
        catch (JsonException ex)
        {
            _userInteractor.PrintError($"JSON in {fileName} file was not " +
                                       $"in a valid format.JSON body:");
            _userInteractor.PrintError(fileContents);
            
            throw new JsonException($"{ex.Message} The file is:{fileName}", ex);
        }
    }
}